package photorganizer.common.context;

import java.util.HashMap;
import java.util.Map;

import photorganizer.common.lifecycle.Closeable;
import photorganizer.common.logging.LoggingHome;

public class SimpleContext implements Context {

	private final Map<Object, Object> map;

	public SimpleContext() {
		map = new HashMap<Object, Object>();
	}

	/* (non-Javadoc)
	 * @see photorganizer.common.context.Context#get(java.lang.Object)
	 */
	public Object get(Object key) {
		return map.get(key);
	}

	/* (non-Javadoc)
	 * @see photorganizer.common.context.Context#put(java.lang.Object, java.lang.Object)
	 */
	public void put(Object key, Object value) {
		map.put(key, value);
	}

	/* (non-Javadoc)
	 * @see photorganizer.common.lifecycle.Closeable#close()
	 */
	public void close() {
		for (Object value : map.values()) {
			if (value instanceof Closeable) {
				try {
					((Closeable) value).close();
				}
				catch (Throwable throwable) {
					LoggingHome.warning("Failed to close ", value, throwable);
				}
			}
		}
		map.clear();
	}

}
